package cn.yhbk.web.modules.manager.Controller;

import cn.hutool.core.bean.BeanUtil;
import cn.yhbk.web.common.Util.MyUtil;
import cn.yhbk.web.common.api.CommonResult;
import cn.yhbk.web.modules.manager.Pojo.DTO.Message.MessageDto;
import cn.yhbk.web.modules.manager.Pojo.Entity.TbMessage;
import cn.yhbk.web.modules.manager.Pojo.VO.Message.MessageAdd;
import cn.yhbk.web.modules.manager.Pojo.VO.Message.MessageUpdate;
import cn.yhbk.web.modules.manager.Service.ITbMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 消息通知表 前端控制器
 * </p>
 *
 * @author 有何不可
 * @since 2023-08-05
 */
@Api(tags = "消息接口")
@Slf4j
@RestController
@RequestMapping("/manager/tbMessage")
public class TbMessageController {

    @Autowired
    private ITbMessageService iTbMessageService;

    @ApiOperation("根据ID查询消息")
    @GetMapping("/getMessageInfo")
    public  CommonResult<MessageDto> getMessageInfo(@RequestParam("id") Long id){
        log.info("查询消息,id:" + id);
        return CommonResult.success(iTbMessageService.queryMessageInfoById(id));
    }

    @ApiOperation("增加消息")
    @PostMapping("/saveMessage")
    public CommonResult<String> saveClass(@RequestBody MessageAdd messageAdd){
        log.info("增加消息，" + messageAdd);
        TbMessage tbMessage = BeanUtil.copyProperties(messageAdd, TbMessage.class);
        tbMessage.setCreateTime(LocalDateTime.now());
        tbMessage.setUpdateTime(LocalDateTime.now());
        tbMessage.setStatus(1);
        // 这里用的是管理员表id，考虑到管理员也可以添加消息
        tbMessage.setCreateUserId(MyUtil.getCurrentAdminId());
        Boolean saveMessage = iTbMessageService.save(tbMessage);
        if(!saveMessage){
            return CommonResult.failed("保存消息出错");
        }
        return CommonResult.success("保存成功");
    }

    @ApiOperation("修改消息")
    @PutMapping("/updateMessage")
    public CommonResult<String> updateMessage(@RequestBody MessageUpdate messageUpdate){
        log.info("修改消息，" + messageUpdate);
        TbMessage tbMessage = iTbMessageService.getById(messageUpdate.getId());
        BeanUtils.copyProperties(messageUpdate, tbMessage);
        tbMessage.setUpdateTime(LocalDateTime.now());
        Boolean updateMessage = iTbMessageService.updateById(tbMessage);
        if(!updateMessage){
            return CommonResult.failed("修改消息出错");
        }
        return CommonResult.success("修改成功");
    }

    @ApiOperation("删除消息")
    @DeleteMapping("/deleteMessage/{messageId}")
    public CommonResult<String> deleteMessage(@PathVariable("messageId") Long messageId){
        log.info("删除消息，id:" + messageId);
        Boolean deleteMessage = iTbMessageService.removeById(messageId);
        if(!deleteMessage){
            return CommonResult.failed("删除消息出错");
        }
        return CommonResult.success("删除成功");
    }

    @ApiOperation("学生设置消息已读")
    @PutMapping("/readMessage")
    public CommonResult<String> readMessage(@RequestBody List<Long> messageIdList){
        log.info("设置已读消息：" + messageIdList);
        Boolean deleteMessage = iTbMessageService.readMessageByIdList(messageIdList);
        if(!deleteMessage){
            return CommonResult.failed("已读消息出错");
        }
        return CommonResult.success("已读消息成功");
    }

}
